const superagent = require('superagent');
const cheerio = require('cheerio');
const fs = require('fs');
const moment = require('moment');

const reptileUrl = `https://etherscan.io/token/generic-tokenholders2?a=0xd97579cea3fe2473682a4c42648134bb982433b9&s=21000000000000000000`;
const start = () => {
    let promises = [];
    for(let p = 1;p <= 20 ;p++) {
        promises.push(new Promise((resolve, reject) => {
            let url = p == 1 ? reptileUrl : `${reptileUrl}&p=${p}`;
            superagent.get(url, (err, res) => {
                // 抛错拦截
                if(err){
                    console.log(err);
                    reject();
                    return;
                }
                let $ = cheerio.load(res.text);
                let results = [];
                $('#maintable tr').each((idx,tr) => {
                    if(idx == 0) return;
                    let tds = $(tr).find('td');
                    results.push(`${$(tds[1]).text()},${$(tds[2]).text()}`);
                });
                resolve(results);
            });
        }))
    }
    Promise.all(promises).then(res => {
        let results = [];
        res.forEach(it => results = results.concat(it));
        console.log(`一共检索到${results.length}条记录`);
        fs.writeFile(__dirname + `/res_${moment().format('YYYYMMDDHH')}.txt`, results.join('\n'), err => err ? console.error(err) : console.log('抓取完成'));
    }).catch(err => {
        console.log(err);
    });
}

const timeInterval = () => {
    setTimeout(() => {
        start();
        timeInterval();
    }, moment().add(2, 'hours').diff(moment()));
}

timeInterval();